﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection;
using System.Windows;
using Caliburn.Micro;
using JY.Common.Extensions;
using JY.Wpf.Core.Events;
using JY.Wpf.Core.Models;
using JY.Wpf.Core.ViewModelBases;
using SD.IOC.Core.Mediators;
using MessageBox = HandyControl.Controls.MessageBox;

namespace JY.Wpf.Test.ViewModels
{
    public class MainMenuViewModel : ViewModelBase
    {
        #region # 变量
        private ObservableCollection<SystemMenu> _systemMenus;
        private SystemMenu _curSystemMenu;
        #endregion

        #region # 构造器
        /// <summary>
        /// 构造器
        /// </summary>
        public MainMenuViewModel()
        {
            this.InitView();
        }
        #endregion

        #region # 属性
        #region # 系统菜单——ObservableCollection<SystemMenu> SystemMenus
        /// <summary>
        /// 系统菜单
        /// </summary>
        public ObservableCollection<SystemMenu> SystemMenus
        {
            get { return this._systemMenus; }
            set { this._systemMenus = value; base.NotifyOfPropertyChange(); }
        }
        #endregion
        #endregion

        #region # Command方法

        #region # 用户信息——void UserInfo()
        /// <summary>
        /// 用户信息
        /// </summary>
        public void UserInfo()
        {
            //this.EventAggregator.PublishOnUIThread(new ShowChildWindowEvent(new LoginViewModel("立高智造", "")));
        }
        #endregion

        #region # 消息——void Message()
        /// <summary>
        /// 消息
        /// </summary>
        public void Message()
        {
            MessageBox.Show("消息");
        }
        #endregion

        #region # 工具——void Tool()
        /// <summary>
        /// 工具
        /// </summary>
        public void Tool()
        {
            MessageBox.Show("工具");
        }
        #endregion

        #region # 设置——void Setting()
        /// <summary>
        /// 设置
        /// </summary>
        public void Setting()
        {
            MessageBox.Show("设置");
        }
        #endregion

        #region # 退出登录——void LogOut()
        /// <summary>
        /// 退出登录
        /// </summary>
        public void LogOut()
        {
            MessageBox.Show("退出登录");
        }
        #endregion

        #region 跳转页面——void Navigate(SystemMenu systemMenu)
        /// <summary>
        /// 跳转页面
        /// </summary>
        /// <param name="systemMenu">菜单项</param>
        public void Navigate(SystemMenu systemMenu)
        {
            this._curSystemMenu = systemMenu;
            if (systemMenu.SubMenus.Any())
            {
                this.SystemMenus = new ObservableCollection<SystemMenu>(systemMenu.SubMenus);
                if (systemMenu.SubMenus.Count() == 1)
                {
                    this.Navigate(systemMenu.SubMenus.First());
                }
            }
            else
            {
                this.IsFlipped = false;

                if (string.IsNullOrWhiteSpace(systemMenu.Url))
                {
                    this.EventAggregator.PublishOnUIThread(new WarningMessageEvent("当前菜单权限不正确！"));
                    return;
                }

                Type type = MainMenuViewModel.GetUrlType(systemMenu.Url);
                if (type == null)
                {
                    this.EventAggregator.PublishOnUIThread(new WarningMessageEvent("当前菜单权限不正确！"));
                    return;
                }
                object content = ResolveMediator.Resolve(type);
                //if (content is WindowViewModelBase windowViewModel)
                //{
                //    windowViewModel.Title = systemMenu.Name;
                //    this.EventAggregator.PublishOnUIThread(new ShowWindowEvent(windowViewModel));
                //}
                //else if (content is ChildWindowViewModelBase childWindowViewModel)
                //{
                //    childWindowViewModel.Title = systemMenu.Name;
                //    this.EventAggregator.PublishOnUIThread(new ShowChildWindowEvent(childWindowViewModel));
                //}
                //else if (content is ViewModelBase viewModel)
                //{
                //    viewModel.Title = systemMenu.Name;
                //    this.EventAggregator.PublishOnUIThread(new SetContentEvent(viewModel));
                //}
                if (content is ViewModelBase viewModel)
                {
                    viewModel.Title = systemMenu.Name;
                    this.EventAggregator.PublishOnUIThread(new SetContentEvent(viewModel));
                }
            }
        }
        #endregion
        #endregion

        #region # 方法

        #region # 初始化页面——void InitView()
        /// <summary>
        /// 初始化页面
        /// </summary>
        public override void InitView()
        {
            List<string> brushStrList=new List<string>()
            {
                "ErrorBrush",
                "WarningBrush",
                "InfoBrush",
                "SuccessBrush",
                "AccentBrush",
                "DangerBrush",
            };
            Random random=new Random();
            ;
            List<SystemMenu> caliburnMenus = new List<SystemMenu>()
            {
                new SystemMenu("Bootstrapper","JY.Wpf.Test.ViewModels.CaliburnDemoViewModels.BootstrapperViewModel",""){ColorBrushStr = brushStrList[NumericExtension.UniqueRandom(brushStrList.Count-1)]},
                new SystemMenu("Actions","JY.Wpf.Test.ViewModels.CaliburnDemoViewModels.ActionsViewModel",""){ColorBrushStr = brushStrList[NumericExtension.UniqueRandom(brushStrList.Count-1)]},
                new SystemMenu("Screens","JY.Wpf.Test.ViewModels.CaliburnDemoViewModels.ScreensViewModel",""){ColorBrushStr = brushStrList[NumericExtension.UniqueRandom(brushStrList.Count-1)]},
                new SystemMenu("EventAggregator","JY.Wpf.Test.ViewModels.CaliburnDemoViewModels.EventAggregatorViewModel",""){ColorBrushStr = brushStrList[NumericExtension.UniqueRandom(brushStrList.Count-1)]},
                new SystemMenu("IResult","JY.Wpf.Test.ViewModels.CaliburnDemoViewModels.ResultViewModel",""){ColorBrushStr = brushStrList[NumericExtension.UniqueRandom(brushStrList.Count-1)]},
            };
            List<SystemMenu> controlMenus = new List<SystemMenu>()
            {
                new SystemMenu("头像","JY.Wpf.Test.ViewModels.ControlDemoViewModels.GravatarDemoViewModel",""){ColorBrushStr = brushStrList[NumericExtension.UniqueRandom(brushStrList.Count-1)]},
                new SystemMenu("按钮","JY.Wpf.Test.ViewModels.ControlDemoViewModels.ButtonsViewModel",""){ColorBrushStr = brushStrList[NumericExtension.UniqueRandom(brushStrList.Count-1)]},
                new SystemMenu("对话气泡","JY.Wpf.Test.ViewModels.ControlDemoViewModels.ChatBoxDemoViewModel",""){ColorBrushStr = brushStrList[NumericExtension.UniqueRandom(brushStrList.Count-1)]},
                new SystemMenu("输入控件","JY.Wpf.Test.ViewModels.ControlDemoViewModels.InputDemoViewModel",""){ColorBrushStr = brushStrList[NumericExtension.UniqueRandom(brushStrList.Count-1)]},
                new SystemMenu("日期时间选择器","JY.Wpf.Test.ViewModels.ControlDemoViewModels.DateDemoViewModel",""){ColorBrushStr = brushStrList[NumericExtension.UniqueRandom(brushStrList.Count-1)]},
                new SystemMenu("搜索栏","JY.Wpf.Test.ViewModels.ControlDemoViewModels.SearchDemoViewModel",""){ColorBrushStr = brushStrList[NumericExtension.UniqueRandom(brushStrList.Count-1)]},
                new SystemMenu("标签","JY.Wpf.Test.ViewModels.ControlDemoViewModels.TagDemoViewModel",""){ColorBrushStr = brushStrList[NumericExtension.UniqueRandom(brushStrList.Count-1)]},
                new SystemMenu("封面视图","JY.Wpf.Test.ViewModels.ControlDemoViewModels.CoverViewDemoViewModel",""){ColorBrushStr = brushStrList[NumericExtension.UniqueRandom(brushStrList.Count-1)]},
                new SystemMenu("进度条","JY.Wpf.Test.ViewModels.ControlDemoViewModels.ProgressBarViewModel",""){ColorBrushStr = brushStrList[NumericExtension.UniqueRandom(brushStrList.Count-1)]},
                new SystemMenu("等待遮罩层","JY.Wpf.Test.ViewModels.ControlDemoViewModels.BusyIndicatorDemoViewModel",""),
                new SystemMenu("滚动块","JY.Wpf.Test.ViewModels.ControlDemoViewModels.RunningBlockDemoViewModel",""){ColorBrushStr = brushStrList[NumericExtension.UniqueRandom(brushStrList.Count-1)]},
                new SystemMenu("图片浏览器","JY.Wpf.Test.ViewModels.ControlDemoViewModels.ImageBrowserDemoViewModel",""){ColorBrushStr = brushStrList[NumericExtension.UniqueRandom(brushStrList.Count-1)]},
                new SystemMenu("时间条","JY.Wpf.Test.ViewModels.ControlDemoViewModels.TimeBarViewModel",""){ColorBrushStr = brushStrList[NumericExtension.UniqueRandom(brushStrList.Count-1)]},
                new SystemMenu("卡片","JY.Wpf.Test.ViewModels.ControlDemoViewModels.CardDemoViewModel",""){ColorBrushStr = brushStrList[NumericExtension.UniqueRandom(brushStrList.Count-1)]},
                new SystemMenu("桌面通知","JY.Wpf.Test.ViewModels.ControlDemoViewModels.NotificationDemoViewModel",""){ColorBrushStr = brushStrList[NumericExtension.UniqueRandom(brushStrList.Count-1)]},
                new SystemMenu("抽屉弹层","JY.Wpf.Test.ViewModels.ControlDemoViewModels.DrawerDemoViewModel",""){ColorBrushStr = brushStrList[NumericExtension.UniqueRandom(brushStrList.Count-1)]},
                new SystemMenu("边框","JY.Wpf.Test.ViewModels.ControlDemoViewModels.BorderDemoViewModel",""){ColorBrushStr = brushStrList[NumericExtension.UniqueRandom(brushStrList.Count-1)]},
                new SystemMenu("选项卡控件","JY.Wpf.Test.ViewModels.ControlDemoViewModels.TabControlDemoViewModel",""){ColorBrushStr = brushStrList[NumericExtension.UniqueRandom(brushStrList.Count-1)]},
                new SystemMenu("树形列表控件","JY.Wpf.Test.ViewModels.ControlDemoViewModels.TreeListViewDemoViewModel",""){ColorBrushStr = brushStrList[NumericExtension.UniqueRandom(brushStrList.Count-1)]},
            };
            List<SystemMenu> MVVMMenus = new List<SystemMenu>()
            {
                new SystemMenu("框架说明","JY.Wpf.Test.ViewModels.MVVMDemoViewModels.InstructionsViewModel",""){ColorBrushStr = brushStrList[NumericExtension.UniqueRandom(brushStrList.Count-1)]},
                new SystemMenu("编码约定","JY.Wpf.Test.ViewModels.MVVMDemoViewModels.ConventionsViewModel",""){ColorBrushStr = brushStrList[NumericExtension.UniqueRandom(brushStrList.Count-1)]},
                //new SystemMenu("MVVM基类","JY.Wpf.Test.ViewModels.MVVMDemoViewModels.BasesViewModel",""),
                //new SystemMenu("项目引用&配置","JY.Wpf.Test.ViewModels.MVVMDemoViewModels.InstructionsViewModel",""),
                new SystemMenu("弹出窗口","JY.Wpf.Test.ViewModels.MVVMDemoViewModels.PopupWinodwViewModel",""){ColorBrushStr = brushStrList[NumericExtension.UniqueRandom(brushStrList.Count-1)]},
                new SystemMenu("弹出子窗口","JY.Wpf.Test.ViewModels.MVVMDemoViewModels.ChildWinodwViewModel",""){ColorBrushStr = brushStrList[NumericExtension.UniqueRandom(brushStrList.Count-1)]},
                new SystemMenu("警报消息","JY.Wpf.Test.ViewModels.MVVMDemoViewModels.WarningMessageViewModel",""){ColorBrushStr = brushStrList[NumericExtension.UniqueRandom(brushStrList.Count-1)]},
                new SystemMenu("消息提示","JY.Wpf.Test.ViewModels.MVVMDemoViewModels.MessageViewModel",""){ColorBrushStr = brushStrList[NumericExtension.UniqueRandom(brushStrList.Count-1)]},
                new SystemMenu("授权测试","JY.Wpf.Test.ViewModels.MVVMDemoViewModels.LicenseViewModel",""){ColorBrushStr = brushStrList[NumericExtension.UniqueRandom(brushStrList.Count-1)]},
                new SystemMenu("分页列表","JY.Wpf.Test.ViewModels.MVVMDemoViewModels.PageTestViewModel",""){ColorBrushStr = brushStrList[NumericExtension.UniqueRandom(brushStrList.Count-1)]},
                new SystemMenu("称量控件","JY.Wpf.Test.ViewModels.MVVMDemoViewModels.BalanceTestViewModel",""){ColorBrushStr = brushStrList[NumericExtension.UniqueRandom(brushStrList.Count-1)]},
                new SystemMenu("翻转控件","JY.Wpf.Test.ViewModels.MVVMDemoViewModels.FlipperTestViewModel",""){ColorBrushStr = brushStrList[NumericExtension.UniqueRandom(brushStrList.Count-1)]},
                new SystemMenu("储位示例","JY.Wpf.Test.ViewModels.MVVMDemoViewModels.StorageAreaTestViewModel",""){ColorBrushStr = brushStrList[NumericExtension.UniqueRandom(brushStrList.Count-1)]},
            };
            List<SystemMenu> kanbanMenus = new List<SystemMenu>()
            {
                new SystemMenu("Scada看板1","",""){ColorBrushStr = brushStrList[NumericExtension.UniqueRandom(brushStrList.Count-1)]},
                new SystemMenu("Scada看板2","",""){ColorBrushStr = brushStrList[NumericExtension.UniqueRandom(brushStrList.Count-1)]},
            };
            this.SystemMenus = new ObservableCollection<SystemMenu>()
            {
                new SystemMenu("Caliburn.MicroDemo"){SubMenus = caliburnMenus},
                new SystemMenu("控件Demo"){SubMenus = controlMenus},
                new SystemMenu("MVVM & 主页面Demo"){SubMenus = MVVMMenus},
                new SystemMenu("Scada看板Demo"){SubMenus = kanbanMenus},
            };
        }
        #endregion

        #region # 获取资源类型——Type GetUrlType(string url)
        /// <summary>
        /// 获取资源类型
        /// </summary>
        /// <param name="url">路径</param>
        /// <returns></returns>
        public static Type GetUrlType(string url)
        {
            try
            {
                var assemblyUrlStr = url.Substring(0, url.IndexOf(".", StringComparison.Ordinal));
                Assembly assembly = Application.Current.GetType().Assembly;
                var assemblyStr = assembly.FullName.Substring(0, assembly.FullName.IndexOf(",", StringComparison.Ordinal));
                if (assemblyStr == assemblyUrlStr)
                {
                    return Type.GetType(url);
                }
                else
                {
                    return Assembly.Load(assemblyUrlStr).ExportedTypes.FirstOrDefault(x => x.ToString() == url);
                }
            }
            catch (Exception)
            {
                return Type.GetType(url);
            }
        }
        #endregion


        #endregion
    }
}
